//
//  YLALoadIngViewController.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/7/15.
//

import Foundation
import UIKit

class YLALoadIngViewController: UIViewController{
    
    var progressView: UIProgressView!
    
    var timer: Timer?
    
    var center: UILabel = {
        let lab = UILabel()
        lab.text = "Loading, please wait…"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 16)
        lab.textAlignment = .center
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var bgImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "img_launch"))
        return image
    }()
    
    lazy var logoImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "logo"))
        return image
    }()
    
    lazy var proVc: UIView = {
        let vc = UIView()
        vc.backgroundColor = .clear
        vc.layer.cornerRadius = 10.0
        vc.layer.borderWidth = 2
        vc.layer.borderColor = UIColor.white.cgColor
        vc.layer.masksToBounds = true
        return vc
    }()
    
   
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.red
        AppOpenAdManager.shared.showAdIfAvailable()
 
        setUI()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    
    func setUI() {
        
        view.addSubview(bgImg)
        bgImg.snp.makeConstraints { make in
            make.left.top.bottom.right.equalToSuperview()
        }
        
        view.addSubview(logoImg)
        logoImg.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(206)
            make.width.height.equalTo(96)
        }
        
        view.addSubview(proVc)
        proVc.snp.makeConstraints { make in
            make.bottom.equalTo(-148)
            make.left.equalTo(62)
            make.right.equalTo(-62)
            make.height.equalTo(20)
        }
        
        view.addSubview(center)
        center.snp.makeConstraints { make in
            make.bottom.equalTo(proVc.snp.top).offset(-11)
            make.centerX.equalToSuperview()
        }
        
        // 设置进度条的颜色为白色
        progressView = UIProgressView()
        progressView.progressTintColor = .white
        // 如果需要的话，设置轨道颜色（默认是灰色）
        progressView.trackTintColor = UIColor.clear // 或者设置为其他颜色
        view.addSubview(progressView)
        progressView.snp.makeConstraints { make in
            make.centerY.equalTo(proVc)
            make.left.equalTo(67)
            make.right.equalTo(-67)
            make.height.equalTo(11)
        }
        
    
                
        // 开始加载进度条
        startLoadingProgress()
        
    }
    
    func startLoadingProgress() {
            // 进度条的初始值设为0
            progressView.progress = 0.0
            
            // 创建一个定时器，每秒更新一次进度（总共8秒）
        timer = Timer.scheduledTimer(timeInterval: 0.0625, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
            
            // 8秒后停止定时器并触发完成方法
            DispatchQueue.main.asyncAfter(deadline: .now() + 8.0) {
                self.stopLoadingProgress()
                self.progressCompleted() // 调用完成方法
            }
        }
    
    @objc func updateProgress() {
            // 更新进度，直到达到100%
            if progressView.progress < 1.0 {
                progressView.progress += 0.015625/2 // 8秒 = 8次 * 1秒/次，所以每次增加1/8 = 0.125
            }
        }
        
        func stopLoadingProgress() {
            // 停止定时器
            timer?.invalidate()
            timer = nil
            
            // 如果需要，可以将进度条设置为100%（通常UI会自动更新到100%）
            // progressView.progress = 1.0
        }
        
        func progressCompleted() {
            
            AppOpenAdManager.shared.showAdIfAvailable()
            
            // 进度条加载完成，触发的方法
            print("进度条加载完成")
            // 在这里执行你需要的操作
            
            //区分是否首次进入
//            if let isSet = UserDefaults.standard.string(forKey: "user_set") {
                // isSet 有一个值，判断是否是会员  会员直接进入主页
                let userDefaults = UserDefaults.standard

//                let isUserStroe = userDefaults.bool(forKey: "isUserStroe")
            let isUserStroe = userDefaults.bool(forKey: "life") || userDefaults.bool(forKey: "year") ||  userDefaults.bool(forKey: "week")
                if !isUserStroe {//不是  去会员开通页面
                    let storeVc = YLAStroeViewControll()
                    storeVc.sourceVc = "store"
                    self.navigationController?.pushViewController(storeVc, animated: true)
                }else{//是 去主页
                    let homeVc = YLAHomeViewController()
                    let nav_1 = UINavigationController(rootViewController: homeVc)
                    var image:UIImage = UIImage(named: "tabbar_home_s")!
                    var selectedimage:UIImage = UIImage(named: "tabbar_home_u")!
                    image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    selectedimage = selectedimage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    nav_1.tabBarItem = UITabBarItem(title: "Home", image: selectedimage, selectedImage: image)
                    
                    let appforVc = YLAListViewController()
                    let nav_2 = UINavigationController(rootViewController: appforVc)
                    var image_2:UIImage = UIImage(named: "tabbar_chat_u")!
                    var selectedimage_2:UIImage = UIImage(named: "tabbar_chat_s")!
                    image_2 = image_2.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    selectedimage_2 = selectedimage_2.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    nav_2.tabBarItem = UITabBarItem(title: "Chat", image: image_2, selectedImage: selectedimage_2)
                    
                    let setVc = YLAMyViewController()
                    let nav_3 = UINavigationController(rootViewController: setVc)
                    var image_3:UIImage = UIImage(named: "tabbar_me_u")!
                    var selectedimage_3:UIImage = UIImage(named: "tabbar_me_s")!
                    image_3 = image_3.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    selectedimage_3 = selectedimage_3.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    nav_3.tabBarItem = UITabBarItem(title: "Me", image: image_3, selectedImage: selectedimage_3)
                    
                    // 全局设置TabBarItem的标题文字颜色（可选）
                    self.tabBarController!.tabBar.tintColor = UIColor.white
                    
                    self.tabBarController!.tabBar.unselectedItemTintColor = UIColor(hex: "D36AF0")
                    
                    self.tabBarController!.viewControllers = [nav_1, nav_2, nav_3]
                }
                
               
//            }else{
//                let setVc = YLASetViewControll()
//                self.navigationController?.pushViewController(setVc, animated: true)
//            }
            
           
            
        }
 
   
    
}

